<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("./_inc/crulFunction.php");

$errMsg = "";
$module = sxREQ('mod');
$name = sxREQ('name');
$alias = sxREQ('alias');

if($module=='' || $name=='') {
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("missing name '$module/$name'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "delete"){
	$errMsg .= crulAliasDel();
}
$sSQL = "SELECT cra.*, e.description AS e_des  "
	.", a.attribute, a.description AS a_des "
	." FROM com_rule_alias cra " 
	." LEFT JOIN entity e ON e.entity = cra.entity "
	." LEFT JOIN attribute a ON a.entity = cra.entity " 
	." WHERE cra.module='".$module."' "
	." AND cra.name='".$name."' "
	." AND cra.com_alias='".$alias."'"
	." ORDER BY a.entity, a.sequence";
$objSDAO->query($sSQL,'crulAlAtt');
if(!$rowA = $objSDAO->fetch('crulAlAtt')){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("missing alias '$module/$name/$alias'"));
	serverRedir($url);
}
sxRDef('description',$rowA['description']);
$aAliasXYW['W'] = 'simple memory variable';
$aAliasXYW['X'] = '1 dimension array memory variables';
$aAliasXYW['Y'] = '2 dimensions array memory variables';

if (isset($aAliasXYW[$alias])) {
	sxRDef('entity','');
} else {
	sxRDef('entity',$rowA['entity']." - ".$rowA['e_des']);
}
sxRDef('read_only',$rowA['read_only']);
sxRDef('note',$rowA['note']);

$sSQL = "SELECT * FROM com_rule_variable " 
	." WHERE module='".$module."' "
	." AND name='".$name."' "
	." AND com_alias='".$alias."'"
	." ORDER BY variable";
$query = $objSDAO->query($sSQL, "crulAlVar");

$sSQL = "SELECT * FROM entity ORDER BY entity";
$objSDAO->query($sSQL);
$aEntity = array();
while($row = $objSDAO->fetch()){
	$aEntity[$row['entity']] = $row['entity'];
}

$aOpz = array("kMenu"=>'pro.cru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function conDel(){
	if (confirm("Confirm ?")) {
		document.location.href="<?="crulAliasView.php?reqtype=delete&mod=$module&name=$name&alias=$alias&xsn=".XSN?>";
	}
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getCrulHeader($module,$name);

$aCmd = array("del"=>array("pag"=>"javascript:confDel()",
										"title"=>"delete",
										"text"=>"delete"
									),
					"chg"=>array("pag"=>"crulAliasMod.php?mod=$module&name=$name&alias=$alias&xsn=".XSN,
										"title"=>"change",
										"text"=>"change"
									),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='4'>Alias</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"15%\">Alias</td>" .
			"<td class=\"text01\" width=\"15%\">".$alias."</td>" .
			"<td class=\"labl01\" width=\"15%\">Read-only</td>" .
			"<td class=\"text01\" width=\"55%\">".
			getHtmlVisDecode("read_only",array('N'=>'No','Y'=>'Yes'))."</td>" .
		"</tr>\n");
if ($alias < 'R') {
	echo("<tr>" .
				"<td class=\"labl01\">Entity</td>" .
				"<td class=\"text01\" colspan='3'>");
	echo getHtmlVisDecode('entity', $aEntity) .
			"</tr>\n";
}
if (sxRTrim('description')!='') {
	echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\" colspan='3'>".
			getHtmlVisText('description')."</td>" .
		"</tr>\n");
}
if (sxRTrim('note')!='') {
	echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\" colspan='3'>".
			getHtmlVisNote("note")."</td>" .
		"</tr>\n");
}
echo("</table>");
if ($alias > 'Q') {
	//list of variables
	echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"crulVarIns.php?mod=$module&name=$name&alias=$alias",
						"title"=>_t("new variable"),
						"text"=>_t("new variable"),
						"perm"=>"viewmod",
						"align"=>"R");
echo getTitleLink('Variables', $aCmd);
		echo("<tr>" .
			"<td class=\"tabh01\" width=\"18%\">Name</td>" .
			"<td class=\"tabh01\" width=\"2%\">R/O</td>" .
			"<td class=\"tabh01\" width=\"80%\">Description</td>" .
		"</tr>\n");	
	while($rowV = $objSDAO->fetch("crulAlVar")){
		echo("<tr>" .
				"<td class=\"text01\" valign='top'><a class=\"text01\" ".
				goToPag("crulVarMod.php?mod=$module&name=$name&alias=$alias&var=".$rowV["variable"]."&xsn=".XSN).
				" title=\"\">$alias".'_'.sxHE($rowV["variable"])."</a></td>\n" .
				"<td class=\"text01\" valign='top'>".(($rowV["read_only"]=='Y')?'R/O':'')."</td>" .
				"<td class=\"text01\" valign='top'>".sxHE($rowV["description"]).
				"<br/>".sxHE($rowV["description"])."</td>" .
			"</tr>\n");
	}
	echo("</table>\n");
} else {
	//list entity-attribute
	echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
			"<td class=\"titl01\"colspan=\"4\">Attributes</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"tabh01\" width=\"19%\">Attribute</td>" .
			"<td class=\"tabh01\" width=\"1%\" align='center'>Read-only</td>" .
			"<td class=\"tabh01\" width=\"50%\">Description</td>" .
		"</tr>\n");	
	while($rowA){
		echo("<tr>" .
			"<td class=\"text01\" valign='top'>".$alias."_" .$rowA["attribute"]."</a></td>\n");
		echo("<td class=\"text01\" valign='top' align='center'>".(($rowA["read_only"]=='Y')?'R/O':'&nbsp;')."</td>" .
				"<td class=\"text01\" valign='top'>".sxHE($rowA["a_des"])."</td>" .
			"</tr>\n");
		$rowA = $objSDAO->fetch("crulAlAtt");
	}
	echo("</table>\n");
}

echo $sCmd;

echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function crulAliasDel(){
 	global $objSDAO;
	$quote = "`";
	$errMsg = "";

	if($errMsg != ""){
		return $errMsg;
	}		
	$sSQL = "DELETE FROM com_rule_alias " .
				" WHERE module = '".sxRAS("mod")."' " .
				" AND name = '".sxRAS("name")."', " .
				" AND coma_alias = ".sxRAS("alias")."'";			
	$objSDAO->exec($sSQL);
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'redir err';
}
